<?php
//后台用户管理
namespace App\Http\Controllers\Admin;

use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator;
use \Illuminate\Mail\Message;
use Mail;
use Hash;

class UserController extends BaseController
{

    //提取到BaseController里面
    //private $_pagesize = 10;
    //public function __construct()
    //{
    //    $this->_pagesize = config('page.pagesize');
    //}

    //用户列表
    public function index(){
        //分页
        $data = User::orderBy('id','ASC')->withTrashed()->paginate($this->pagesize);

        return view('admin.user.index',compact('data'));
    }


    //添加用户显示
    public function create(){
        return view('admin.user.create');
    }
    //添加用户处理
    public function store(Request $request){
        //$this->validate($request,[
        //   'username'=>'required',
        //    'truename'=>'required',
        //    'password'=>'required|confirmed',
        //    'phone'=>'nullable|phone'
        //],[
        //    'truename.required'=>'真实名不能为空',
        //    'phone.phone'=>'手机号码不合法'
        //]);
        //echo 'ddd';die;
       $this->validate($request,[
            'username'=>'required|unique:users,username',
            'truename'=>'required',
            'password'=>'required|confirmed',
            'phone'=>'nullable|phone'
        ]);

        //获取表单数据
        $post = $request->except(['_token','password_confirmation', '/admin/user/add']);
        if(!empty($post['password'])){
            $post['password'] = bcrypt($post['password']);
        }
        //添加数据入库
        $userModel = User::create($post);


        /*//发邮件给用户
        //密码
        $pwd = $post['password'];
        //发送富文本
        \Mail::send('mail.useradd',compact('userModel','pwd'),function(Message $message) use ($userModel){
            //发给谁
            $message->to($userModel->email);
            //主题
            $message->subject('开通账号邮件通知');
        });*/


        //跳转到列表页
        return redirect(route('admin.user.index'))->with('success','添加用户成功');

    }

    //删除用户操作
    public function del(int $id){
        //删除, 结合模型软删除
        User::find($id)->delete();

        //强制删除, 在配置了软删除的时候的,真实删除
        //User::find($id)->forceDelete();

        return ['status'=>0,'msg'=>'删除成功'];
    }

    //还原
    public function restore(int $id){
        User::onlyTrashed()->where('id',$id)->restore();
        return redirect(route('admin.user.index'))->with('success','还原成功');
    }

    //全选删除
    public function delall(Request $request){
        $ids = $request->get('id');
        User::destroy($ids);
        return ['status'=>0,'msg'=>'全选删除成功'];
    }


    //修改显示
    public function edit(int $id){
        $model = User::find($id);

        return view('admin.user.edit',compact('model'));
    }
    //修改处理
    public function update(Request $request,int $id){

        $model = User::find($id);

        //原密码 明文
        $spass = $request->get('spassword');

        //原密码 密文
        $oldpass = $model->password;
        //检查 明文和密码是否一致
        //var_dump($spass,$oldpass);die;
        $bool = Hash::check($spass,$oldpass);


        if($bool){
            //修改
            $data = $request->only([
                'truename',
                'password',
                'phone',
                'sex',
                'email'
            ]);
            if(!empty($data['password'])){
                $data['password'] = bcrypt($data['password']);
            }else{
                unset($data['password']);
            }

            $model->update($data);
            return redirect(route('admin.user.index'))->with('success','修改用户成功');
        }
        return redirect(route('admin.user.edit',$model))->withErrors(['error'=>'原密码不正确']);
    }

    //分配角色和处理
    public function role(Request $request,User $user){
        //判断是否post
        if($request->isMethod('post')){
            $post = $this->validate($request,[
                'role_id'=>'required|min:1'
            ],['role_id.required'=>'必须选择']);

            $user->update($post);
            return redirect(route('admin.user.index'));
        }

        //读取所有的角色
        $roleAll = Role::all();

        return view('admin.user.role',compact('user','roleAll'));
    }

}
